package com.blogsoho.taurus.dao.builder;

import com.blogsoho.taurus.vo.AttachmentVo;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * Created by 16978 on 2017/6/17.
 */
public class AttachmentSqlBuilder {

    public static final String JPRESS_ATTACHMENT = "jpress_attachment";

    public String addAttachment(AttachmentVo attachment) {
        return new SQL() {{
            INSERT_INTO("jpress_attachment");
            VALUES("title", "#{title}");
            VALUES("content_id", "#{contentId}");
            VALUES("md5", "#{md5}");
            VALUES("user_id", "#{userId}");
            VALUES("path", "#{path}");
            VALUES("real_path", "#{realPath}");
            VALUES("suffix", "#{suffix}");
        }}.toString();
    }

    public String getAttachmentByMD5(Map map) {
        return new SQL() {{
            SELECT("*");
            FROM(JPRESS_ATTACHMENT);
            WHERE("md5='" + map.get("md5") + "'");


        }}.toString() + " limit 1";
    }

    public String addAttachmentMapping(Map<String, Integer> map) {
        return new SQL() {{
            INSERT_INTO("taurus_attachment_content");
            VALUES("attachment_id", map.get("aid").toString());
            VALUES("content_id", map.get("cid").toString());
        }}.toString();
    }

    public String getAllAttachment(Map<String, Object> para) {
        String sql = new SQL() {{
            SELECT("*");
            FROM(JPRESS_ATTACHMENT);


        }}.toString();
        sql = sql + " limit " + para.get("start") + "," + para.get("length");
        return sql;
    }

    public String getCount() {
        return new SQL() {{
            SELECT("count(*)");
            FROM(JPRESS_ATTACHMENT);
        }}.toString();
    }
}
